#ifndef __KEYBOARD_H
#define __KEYBOARD_H
#include "sys.h"


typedef struct {
    uint8_t KB_R1;
    uint8_t KB_R2;
    uint8_t KB_R3;
    uint8_t KB_R4;
    uint8_t KB_C1;
    uint8_t KB_C2;
    uint8_t KB_C3;
    uint8_t KB_C4;
} KEYBOARD_ROW_COL_STATE;




#define     KEY_BD_ROW1_GPIO            GPIOA
#define     KEY_BD_ROW1_PIN             GPIO_PIN_7
#define     KEY_BD_ROW2_GPIO            GPIOA
#define     KEY_BD_ROW2_PIN             GPIO_PIN_6
#define     KEY_BD_ROW3_GPIO            GPIOA
#define     KEY_BD_ROW3_PIN             GPIO_PIN_5
#define     KEY_BD_ROW4_GPIO            GPIOA
#define     KEY_BD_ROW4_PIN             GPIO_PIN_4

#define     KEY_BD_COL1_GPIO            GPIOA
#define     KEY_BD_COL1_PIN             GPIO_PIN_3
#define     KEY_BD_COL2_GPIO            GPIOA
#define     KEY_BD_COL2_PIN             GPIO_PIN_2
#define     KEY_BD_COL3_GPIO            GPIOA
#define     KEY_BD_COL3_PIN             GPIO_PIN_1
#define     KEY_BD_COL4_GPIO            GPIOA
#define     KEY_BD_COL4_PIN             GPIO_PIN_0

#define     KEY_BD_RCC_INIT()           __HAL_RCC_GPIOA_CLK_ENABLE()

#define     KEY_BD_ROW1(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_ROW1_GPIO, KEY_BD_ROW1_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_ROW1_GPIO, KEY_BD_ROW1_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_ROW2(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_ROW2_GPIO, KEY_BD_ROW2_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_ROW2_GPIO, KEY_BD_ROW2_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_ROW3(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_ROW3_GPIO, KEY_BD_ROW3_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_ROW3_GPIO, KEY_BD_ROW3_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_ROW4(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_ROW4_GPIO, KEY_BD_ROW4_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_ROW4_GPIO, KEY_BD_ROW4_PIN, GPIO_PIN_RESET); \
                                            } while(0U)

                                            
#define     KEY_BD_COL1(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_COL1_GPIO, KEY_BD_COL1_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_COL1_GPIO, KEY_BD_COL1_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_COL2(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_COL2_GPIO, KEY_BD_COL2_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_COL2_GPIO, KEY_BD_COL2_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_COL3(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_COL3_GPIO, KEY_BD_COL3_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_COL3_GPIO, KEY_BD_COL3_PIN, GPIO_PIN_RESET); \
                                            } while(0U)
#define     KEY_BD_COL4(x)              do{x ? \
                                            HAL_GPIO_WritePin(KEY_BD_COL4_GPIO, KEY_BD_COL4_PIN, GPIO_PIN_SET) : \
                                            HAL_GPIO_WritePin(KEY_BD_COL4_GPIO, KEY_BD_COL4_PIN, GPIO_PIN_RESET); \
                                            } while(0U)

#define     KEY_BD_ROW1_IN              HAL_GPIO_ReadPin(KEY_BD_ROW1_GPIO, KEY_BD_ROW1_PIN)
#define     KEY_BD_ROW2_IN              HAL_GPIO_ReadPin(KEY_BD_ROW2_GPIO, KEY_BD_ROW2_PIN)
#define     KEY_BD_ROW3_IN              HAL_GPIO_ReadPin(KEY_BD_ROW3_GPIO, KEY_BD_ROW3_PIN)
#define     KEY_BD_ROW4_IN              HAL_GPIO_ReadPin(KEY_BD_ROW4_GPIO, KEY_BD_ROW4_PIN)

void keyboard_init(void);



#endif

